package com.example.control;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Resource
    private RestTemplate restTemplate;

    @RequestMapping(value = "/add.json")
    public String add() {
        System.out.println("添加订单!!");
//        String result = restTemplate.getForObject("http://localhost:8081/stock/deduct", String.class);
        return "order sentinel!!";
    }

    @RequestMapping(value = "/flow.json")
    @SentinelResource(value = "flow",blockHandler = "flowBlockHandler")
    public String flow(){
        return "flow success!!";
    }

    @RequestMapping(value = "/flowThread.json")
    @SentinelResource(value = "flow",blockHandler = "flowBlockHandler" )
    public String flowThread(){
        try {
            Thread.sleep(3*1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "flowThread success!!";
    }


    public String  flowBlockHandler(BlockException e){
        return "流控";
    }
}
